/*************************
 * 派乐科技馆
 * NXT声音传感器
 * 2019.09.15
 *************************/

#include <SoftwareSerial.h>
#include <EV3UARTEmulation.h>
#include <Arduino.h>
EV3UARTEmulation sensor(3, 4, 3, 9600);

void setup() 
{
  sensor.create_mode("dB", true, DATA16, 1, 3, 0);
  sensor.create_mode("dBa", true, DATA16, 1, 3, 0);
  sensor.reset();
}

unsigned long last_reading = 0;

void loop() 
{
  int n = analogRead(A0);
  //int n = 353;
  sensor.heart_beat();
  if (millis() - last_reading > 100) 
  {
    float x = (100 - n) / 0.02442;
    switch (sensor.get_current_mode()) 
    {
      case 0:
        sensor.send_data16(0, x); 
        break;
      case 1:
        sensor.send_data16(1, x); 
        break;
    }
    last_reading = millis();
  }
}
